import 'package:flutter/foundation.dart';

/// 心理测试类型枚举
enum TestType {
  SDS,  // 抑郁自评量表
  SAS,  // 焦虑自评量表
  SCL90 // 症状自评量表
}

/// 测试记录模型
class TestRecord {
  final int? id;
  final TestType testType;
  final DateTime testDate;
  final List<int> answers;
  final double rawScore;
  final double standardScore;
  final String conclusion;
  final String suggestion;

  TestRecord({
    this.id,
    required this.testType,
    required this.testDate,
    required this.answers,
    required this.rawScore,
    required this.standardScore,
    required this.conclusion,
    required this.suggestion,
  });

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'test_type': testType.index,
      'test_date': testDate.toIso8601String(),
      'answers': answers.join(','),
      'raw_score': rawScore,
      'standard_score': standardScore,
      'conclusion': conclusion,
      'suggestion': suggestion,
    };
  }

  factory TestRecord.fromMap(Map<String, dynamic> map) {
    return TestRecord(
      id: map['id'],
      testType: TestType.values[map['test_type']],
      testDate: DateTime.parse(map['test_date']),
      answers: map['answers'].split(',').map<int>((e) => int.parse(e)).toList(),
      rawScore: map['raw_score'],
      standardScore: map['standard_score'],
      conclusion: map['conclusion'],
      suggestion: map['suggestion'],
    );
  }
}

/// 测试题目模型
class TestQuestion {
  final int id;
  final String content;
  final List<String> options;

  const TestQuestion({
    required this.id,
    required this.content,
    required this.options,
  });
}

/// SDS测试数据
class SDSTest {
  static const List<TestQuestion> questions = [
    TestQuestion(
      id: 1,
      content: '我感到情绪沮丧，郁闷',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 2,
      content: '早晨是我最好的时光',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 3,
      content: '我会哭泣或想哭',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 4,
      content: '我晚上睡眠不好',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 5,
      content: '我吃得和平常一样多',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 6,
      content: '我与异性接触时仍感到愉快',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 7,
      content: '我发现我的体重在下降',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 8,
      content: '我有便秘的苦恼',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 9,
      content: '我的心跳比平常快',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 10,
      content: '我无缘无故感到疲乏',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 11,
      content: '我的头脑和平常一样清楚',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 12,
      content: '我觉得做事和平常一样容易',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 13,
      content: '我感到不安，难以保持平静',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 14,
      content: '我对未来抱有希望',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 15,
      content: '我比平常更容易激动',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 16,
      content: '我觉得做决定很容易',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 17,
      content: '我感到自己是有用的和不可缺少的人',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 18,
      content: '我的生活很有意义',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 19,
      content: '我认为如果我死了别人会过得更好',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 20,
      content: '我仍旧喜欢做平常爱做的事',
      options: ['很少', '有时', '经常', '持续'],
    ),
  ];

  static const Map<String, String> scoreRanges = {
    '正常': '标准分 < 50',
    '轻度抑郁': '50 ≤ 标准分 < 60',
    '中度抑郁': '60 ≤ 标准分 < 70',
    '重度抑郁': '标准分 ≥ 70',
  };

  static String getConclusion(double standardScore) {
    if (standardScore < 50) return '正常';
    if (standardScore < 60) return '轻度抑郁';
    if (standardScore < 70) return '中度抑郁';
    return '重度抑郁';
  }

  static String getSuggestion(String conclusion) {
    switch (conclusion) {
      case '正常':
        return '您的心理状态良好，建议继续保持积极乐观的生活态度。';
      case '轻度抑郁':
        return '建议适当进行运动、放松，保持规律的作息，必要时寻求心理咨询。';
      case '中度抑郁':
        return '建议及时寻求专业的心理咨询帮助，采取适当的干预措施。';
      case '重度抑郁':
        return '建议立即就医，在专业医生指导下进行系统治疗。';
      default:
        return '';
    }
  }

  static double calculateStandardScore(List<int> answers) {
    double rawScore = 0;
    for (int i = 0; i < answers.length; i++) {
      rawScore += answers[i];
    }
    return rawScore * 1.25; // SDS标准分 = 原始分 * 1.25
  }
}

/// SAS测试数据
class SASTest {
  static const List<TestQuestion> questions = [
    TestQuestion(
      id: 1,
      content: '我感到比平常更加紧张和焦虑',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 2,
      content: '我无缘无故地感到害怕',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 3,
      content: '我容易心烦意乱或感到恐慌',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 4,
      content: '我感到我的身体好像要崩溃了',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 5,
      content: '我感到一切都很好，不会发生什么不幸',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 6,
      content: '我的手脚发抖打颤',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 7,
      content: '我因头痛、颈痛和背痛而烦恼',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 8,
      content: '我感到虚弱，容易疲劳',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 9,
      content: '我感到心平气和，并且容易安静地坐着',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 10,
      content: '我感到我的心跳很快',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 11,
      content: '我因阵阵头晕而烦恼',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 12,
      content: '我有晕倒发作或觉得要晕倒似的',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 13,
      content: '我呼吸时感到很容易',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 14,
      content: '我的手脚麻木和刺痛',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 15,
      content: '我因胃痛和消化不良而烦恼',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 16,
      content: '我常常要小便',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 17,
      content: '我的手常常是干燥温暖的',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 18,
      content: '我脸红发热',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 19,
      content: '我容易入睡并且一夜睡得很好',
      options: ['很少', '有时', '经常', '持续'],
    ),
    TestQuestion(
      id: 20,
      content: '我做恶梦',
      options: ['很少', '有时', '经常', '持续'],
    ),
  ];

  static const Map<String, String> scoreRanges = {
    '正常': '标准分 < 50',
    '轻度焦虑': '50 ≤ 标准分 < 60',
    '中度焦虑': '60 ≤ 标准分 < 70',
    '重度焦虑': '标准分 ≥ 70',
  };

  static String getConclusion(double standardScore) {
    if (standardScore < 50) return '正常';
    if (standardScore < 60) return '轻度焦虑';
    if (standardScore < 70) return '中度焦虑';
    return '重度焦虑';
  }

  static String getSuggestion(String conclusion) {
    switch (conclusion) {
      case '正常':
        return '您的心理状态良好，建议继续保持。';
      case '轻度焦虑':
        return '建议学习放松技巧，适当运动，保持规律作息。';
      case '中度焦虑':
        return '建议寻求心理咨询师帮助，学习焦虑管理技巧。';
      case '重度焦虑':
        return '建议及时就医，接受专业医生的系统治疗。';
      default:
        return '';
    }
  }

  static double calculateStandardScore(List<int> answers) {
    double rawScore = 0;
    for (int i = 0; i < answers.length; i++) {
      rawScore += answers[i];
    }
    return rawScore * 1.25; // SAS标准分 = 原始分 * 1.25
  }
}

/// SCL-90测试数据
class SCL90Test {
  static const List<TestQuestion> questions = [
    TestQuestion(id: 1, content: '头痛', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 2, content: '神经过敏，心中不踏实', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 3, content: '头脑中有不必要的想法或字句盘旋', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 4, content: '头昏或昏倒', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 5, content: '对异性的兴趣减退', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 6, content: '对旁人责备求全', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 7, content: '感到别人能控制您的思想', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 8, content: '责怪别人制造麻烦', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 9, content: '忘记性大', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 10, content: '担心自己的衣饰整齐及仪态的端正', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 11, content: '容易烦恼和激动', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 12, content: '胸痛', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 13, content: '害怕空旷的场所或街道', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 14, content: '感到自己的精力下降，活动减慢', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 15, content: '想结束自己的生命', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 16, content: '听到旁人听不到的声音', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 17, content: '发抖', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 18, content: '感到大多数人都不信任您', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 19, content: '胃口不好', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 20, content: '容易哭泣', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 21, content: '同异性相处时感到害羞不自在', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 22, content: '感到受骗、中了圈套或有人想抓住您', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 23, content: '无缘无故地突然感到害怕', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 24, content: '自己不能控制地大发脾气', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 25, content: '怕单独出门', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 26, content: '经常责怪自己', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 27, content: '腰痛', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 28, content: '感到难以完成任务', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 29, content: '感到孤独', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 30, content: '感到苦闷', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 31, content: '过分担忧', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 32, content: '对事物不感兴趣', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 33, content: '感到害怕', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 34, content: '您的感情容易受到伤害', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 35, content: '旁人能知道您的私下想法', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 36, content: '感到别人不理解您、不同情您', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 37, content: '感到人们对您不友好，不喜欢您', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 38, content: '做事必须做得很慢以保证做得正确', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 39, content: '心跳得很厉害', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 40, content: '恶心或胃部不舒服', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 41, content: '感到比不上他人', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 42, content: '肌肉酸痛', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 43, content: '感到有人在监视您、谈论您', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 44, content: '难以入睡', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 45, content: '做事必须反复检查', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 46, content: '难以作出决定', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 47, content: '怕乘电车、公共汽车等交通工具', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 48, content: '呼吸困难', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 49, content: '一阵阵发冷或发热', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 50, content: '因为感到害怕而避开某些东西、场合或活动', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 51, content: '脑子变空了', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 52, content: '身体发麻或刺痛', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 53, content: '喉咙有梗塞感', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 54, content: '感到前途没有希望', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 55, content: '不能集中注意力', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 56, content: '身体某些部位感到虚弱', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 57, content: '感到紧张或容易紧张', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 58, content: '感到手臂或腿很重', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 59, content: '想到死亡的事', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 60, content: '吃得太多', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 61, content: '当别人看着您或谈论您时感到不自在', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 62, content: '有一些不属于您自己的想法', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 63, content: '有想打人或伤害他人的冲动', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 64, content: '醒得太早', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 65, content: '必须反复洗手、点数或触摸某些东西', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 66, content: '睡得不稳不深', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 67, content: '有想摔坏或破坏东西的冲动', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 68, content: '有一些别人没有的想法或念头', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 69, content: '感到对别人神经过敏', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 70, content: '在商店或电影院等公共场所感到不自在', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 71, content: '感到任何事情都很困难', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 72, content: '一阵阵恐惧或惊恐', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 73, content: '感到在公共场合吃东西很不舒服', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 74, content: '经常与人争论', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 75, content: '单独一人时神经很紧张', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 76, content: '别人对您的成绩没有作出恰当的评价', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 77, content: '即使和别人在一起也感到孤单', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 78, content: '感到坐立不安心神不定', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 79, content: '感到自己没有什么价值', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 80, content: '感到熟悉的东西变得陌生或不真实', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 81, content: '大叫或摔东西', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 82, content: '害怕会在公共场合昏倒', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 83, content: '感到别人想占您的便宜', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 84, content: '为一些有关性的想法而很苦恼', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 85, content: '您认为应该因为自己的过错而受到惩罚', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 86, content: '感到要很快把事情做完', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 87, content: '感到自己的身体有严重问题', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 88, content: '从未感到和其他人很亲近', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 89, content: '感到自己有罪', options: ['从无', '轻度', '中度', '偏重', '严重']),
    TestQuestion(id: 90, content: '感到自己的脑子有毛病', options: ['从无', '轻度', '中度', '偏重', '严重']),
  ];

  static const Map<String, List<String>> factors = {
    '躯体化': ['1', '4', '12', '27', '40', '42', '48', '49', '52', '53', '56', '58'],
    '强迫症状': ['3', '9', '10', '28', '38', '45', '46', '51', '55', '65'],
    '人际关系敏感': ['6', '21', '34', '36', '37', '41', '61', '69', '73'],
    '抑郁': ['5', '14', '15', '20', '22', '26', '29', '30', '31', '32', '54', '71', '79'],
    '焦虑': ['2', '17', '23', '33', '39', '57', '72', '78', '80', '86'],
    '敌对': ['11', '24', '63', '67', '74', '81'],
    '恐怖': ['13', '25', '47', '50', '70', '75', '82'],
    '偏执': ['8', '18', '43', '68', '76', '83'],
    '精神病性': ['7', '16', '35', '62', '77', '84', '85', '87', '88', '90'],
    '其他': ['19', '44', '59', '60', '64', '66', '89'],
  };

  static Map<String, double> calculateFactorScores(List<int> answers) {
    Map<String, double> scores = {};
    factors.forEach((factor, items) {
      double sum = 0;
      for (String item in items) {
        sum += answers[int.parse(item) - 1];
      }
      scores[factor] = sum / items.length;
    });
    return scores;
  }

  static String getConclusion(Map<String, double> factorScores) {
    List<String> concerns = [];
    factorScores.forEach((factor, score) {
      if (score >= 2) {
        concerns.add(factor);
      }
    });
    
    if (concerns.isEmpty) {
      return '心理状态正常';
    } else {
      return '在${concerns.join("、")}等方面存在一定程度的心理困扰';
    }
  }

  static String getSuggestion(Map<String, double> factorScores) {
    List<String> suggestions = [];
    factorScores.forEach((factor, score) {
      if (score >= 2) {
        switch (factor) {
          case '躯体化':
            suggestions.add('建议关注身心健康，适当运动放松，必要时进行身体检查。');
            break;
          case '强迫症状':
            suggestions.add('建议学习放松技巧，培养积极思维方式，必要时寻求心理咨询。');
            break;
          // ... 其他因子的建议
        }
      }
    });
    
    if (suggestions.isEmpty) {
      return '建议继续保持良好的心理状态，适当关注自我心理健康。';
    } else {
      return suggestions.join('\n');
    }
  }
} 